<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Mail;
use Crypt;

class ForgetController extends Controller
{
    //找回密码
    public function forget()
    {
    	return view('home.user.forget');
    }

    public function doforget(Request $request)
    {
    	//获取
    	$data = $request -> input();
    	//查询
    	$res = DB::table('users') -> where('email', $data['email']) -> first();
    	//判断
    	if(!$res)
    	{
    		
	    		return back() -> with(['info' => '找不到您的邮箱']);
	    	
    	}
	    	

    	Mail::send('home.email.links', ['user' => $res], function($messages)
			use($res){
    			 $messages->to($res -> email)->subject('找回密码');
    		});

    	//处理邮箱地址
    	$email = $res -> email;
    	$url=trim(strrchr($email, '@'), '@');
    	$url = 'mail.'.$url;
    	return view('home.email.linkmail', ['data' => $url]);
    }

    public function inputpass($id, $token)
    {
    	$res = DB::table('users') -> where('id', $id) -> first();
    	if($res -> token != $token)
    	{
    		return '不合法来源,谨慎操作！';
    	}


    	return view('home.email.inputpass',['data' => $id]);
    }

    public function update(Request $request)
    {
    	
    	if($request -> password != $request -> repassword)
    	{
    		return back() -> with(['info' => '密码不一致']);
    	}
    	//z加密
    	$password = Crypt::encrypt($request -> password);
    	$res = DB::table('users') -> where('id', $request -> id) -> update(['password' => $password]);
    	//j判断
    	if(!$res)
    	{
    		return back() -> with(['info' => '密码修改失败']);
    	}
    	return view('home.email.info') -> with(['info' => '密码修改成功']);

    }
}
